//列表页面  查询按钮  start
$('#listQueryBtn').click(function () {
    var params = getSearchParams($(this));
    if (params) {
        getReservationList(params);
    }
});
//列表页面  查询按钮  end

//预约列表 获取数据 方法  start
function getReservationList(result) {
    ajaxGet(RESERVATIONCENTERURL.reservationList.index, result, function (data) {
        if (data.code === 10000) {
            var str = '';
            for (var i = 0; i < data.data.length; i++) {

                str += ' <tr data-reservationId="' + data.data[i].id + '" data-status="' + data.data[i].status.key + '">' +
                    '<td><lable><input type="checkbox" class="checkBox" data-patientId="' + data.data[i].patient_id + '">' + (i + 1) + '</lable></td>' +
                    '<td>' + data.data[i].patient.name + '</td>' +
                    '<td>' + data.data[i].patient.sex.value + '</td><td>' + data.data[i].patient.age + '</td>' +
                    '<td>' + data.data[i].patient.phone + '</td>' +
                    '<td>' + data.data[i].time + ' ' + data.data[i].section + '</td>' +
                    '<td>' + data.data[i].doctor.name + '</td>' +
                    '<td><span class="clinic">' + data.data[i].room + '</span>诊室</td>' +
                    '<td class="statusText">' + data.data[i].status.value + '</td>' +
                    '<td>' +
                    '<span class="btn btn-outline btn-primary margin-right-5 viewCase" data-patient-id="' + data.data[i].patient.id + '">查看病历</span>' +
                    '<span class="btn btn-outline btn-warning margin-right-5 addSubsequent" data-patient-id="' + data.data[i].patient.id + '">添加复诊</span>' +
                    '</td></tr>'
            }
            $('#reservationListBody').html(str);
            // 页码
            if (data.meta.pagination.total_pages > 1) {
                page('listPager', data.meta, getReservationList, result);
            } else {
                $('#listPager').html('');
            }
            $('#reservationCenterNav li[data-page="reservationList"]').attr('data-load', 'yes');//改变标签栏状态
        } else if (data.code === 40100) {
            aClick();
        } else if (data.code === '40001') {
            $('#reservationListBody').html('<tr><td colspan="10">暂无数据</td></tr>');
            $('#listPager').html('');
        } else {
            alertModel('', '获取失败，' + data.message);
        }
    }, function (x, t, e) {
    })
};
//预约列表 获取数据 方法  end

//操作栏  所有点击按钮  获取要修改的预约 ID  start
function getReservationId(tableId, status) {
    var patientArr = [];
    var inputCheckBox = $('#' + tableId).find('td input.checkBox');

    for (var i = 0; i < inputCheckBox.length; i++) {
        var tr = $(inputCheckBox[i]).parent().parent().parent();
        var condition = status ? (tr.attr('data-status') !== status) : true;
        if ($(inputCheckBox[i]).is(':checked') && condition) {
            patientArr.push(tr.attr('data-reservationId'));
        }
    }
    return patientArr;
};
//操作栏  所有点击按钮  获取要修改的预约 ID  end

//添加预约  start
$('#listOperationBtnBox .addReservationBtn').click(function () {
    window.location.href = $(this).attr('data-url');
});
//添加预约  end

//修改诊室  点击  start
$('#listOperationBtnBox .editClinicBtn').click(function () {
    var reservationIdArr = getReservationId('reservationListBody');
    if (reservationIdArr.length) {
        $('#editClinic').modal('show');  //打开修改诊室模态框
        $('#editClinicSaveBtn').attr('data-reservationIdArr', reservationIdArr);
        $('#editClinicSaveBtn').attr('data-tableId', 'reservationListBody');
    } else {
        alertModel('', '请先选择患者！');
    }
});
//修改诊室  点击  end

//解除预约  点击  start
$('#listOperationBtnBox .removeReservationBtn').click(function () {
    var reservationIdArr = getReservationId('reservationListBody', 2);
    if (reservationIdArr.length) {
        confirmModel('', '确定要解除吗？', '', '', function () {
            ajaxPost(RESERVATIONCENTERURL.reservationList.destroy, {ids: reservationIdArr}, function (data) {
                if (data.code == 10000) {
                    for (var i = 0; i < reservationIdArr.length; i++) {
                        $('#reservationListBody').find('tr[data-reservationId="' + reservationIdArr[i] + '"]').remove();
                    }
                    alertModel('', '解除成功！');
                } else if (data.code === 40100) {
                    aClick();
                } else {
                    alertModel('', '解除失败，' + data.message);
                }
            }, function () {
            })
        });
    } else {
        alertModel('', '请先选择患者！');
    }
});
//解除预约  点击  end

//推送消息和发送短信  点击  和 发送请求  start
$('#listOperationBtnBox .sendAppMessageBtn').click(function () {
    var reservationIdArr = getReservationId('reservationListBody');
    if (reservationIdArr.length) {
        sendMessage(url, reservationIdArr);
    } else {
        alertModel('', '请先选择患者！');
    }
});
$('#listOperationBtnBox .sendPhoneMessageBtn').click(function () {
    var reservationIdArr = getReservationId('reservationListBody', 1);
    if (reservationIdArr.length) {
        sendMessage(RESERVATIONCENTERURL.reservationList.notify, reservationIdArr);
    } else {
        alertModel('', '请先选择患者！');
    }
});

function sendMessage(url, reservationIdArr) {
    confirmModel('', '确定要发送吗？', '', '', function () {
        ajaxPost(url, {ids: reservationIdArr}, function (data) {
            if (data.code == 10000) {
                var body = $('#reservationListBody');
                $.each(reservationIdArr, function (i, v) {
                    body.find('tr[data-reservationid=' + v + ']').find('td.statusText').html('已发送');
                });
                alertModel('', '发送成功！');
            } else if (data.code === 40100) {
                aClick();
            } else {
                alertModel('', '发送失败，' + data.message);
            }
        }, function () {
        })
    });
};
//推送消息和发送短信  点击  和 发送请求  end

//修改诊室弹出框   保存点击  start
$('#editClinicSaveBtn').click(function () {
    var reservationIdArr = $(this).attr('data-reservationIdArr').split(',');
    for (var j = 0; j < reservationIdArr.length; j++) {
        reservationIdArr[j] = parseInt(reservationIdArr[j]);
    }
    var room = $(this).parent().prev().find('select').val();
    $(this).prev().click();  //关闭模态框
    ajaxPost(RESERVATIONCENTERURL.reservationList.update, {ids: reservationIdArr, room: room}, function (data) {
        if (data.code == 10000) {
            for (var i = 0; i < reservationIdArr.length; i++) {
                $('#reservationListBody').find('tr[data-reservationId="' + reservationIdArr[i] + '"] td span.clinic').html(room);
            }
            alertModel('', '修改成功！');
        } else if (data.code === 40100) {
            aClick();
        } else {
            alertModel('', '修改失败，' + data.message);
        }
    }, function () {
    });
});
//修改诊室弹出框   保存点击  end

//查看病历按钮  点击 start
$('#reservationListBody').delegate('td span.viewCase', 'click', function () {
    var id = $(this).attr('data-patient-id');

    $.get('/manage/record/list/' + id, function (data) {
        var str = '';
        $.each(data, function (i, v) {
            str += '<option value="' + v.id + '">' + v.visit_type.value + v.clinic_at + '</option>';
        });

        $('#witch_record').html(str).change();
        $('#viewCaseModal').modal('show');
    }, 'json');
})
// 添加复诊 点击按钮  start
    .delegate('.addSubsequent', 'click', function () {
        var patientId = $(this).attr('data-patient-id');   //获取病人id
        sessionStorage.removeItem('tabPage');
        sessionStorage.removeItem('record_id');
        sessionStorage.setItem('patient_id', patientId);
        window.location.href = '/manage/subsequent';
    });
//  病历选择后的change事件
$('#witch_record').change(function () {
    $.get('/manage/record/print/show', {record_id: $(this).val()}, function (data) {
        if (data.code === 40100) {
            aClick();
        } else {
            $('#viewCaseModal').find('.modal-body').html(data);

        }
    }, 'html');
});

//查看病历按钮  点击 end

//获取查询搜索条件方法  start   todo 患者池和预约列表通用
function getSearchParams(_this) {
    var serializeArr = $('#' + _this.attr('data-id')).find('div.dataSerialize');
    var params = {}; //存储查询时需要提交的数据
    for (var i = 0; i < serializeArr.length; i++) {
        var keyname = '';
        if ($(serializeArr[i]).find('.form-control').length === 1) {
            keyname = $(serializeArr[i]).find('.form-control').attr('name');
            params[keyname] = $(serializeArr[i]).find('.form-control').val();
        } else {
            params[$(serializeArr[i]).find('.form-control').attr('data-name')] = [];
            var inputArr = $(serializeArr[i]).find('.form-control');
            for (var j = 0; j < inputArr.length; j++) {
                params[$(serializeArr[i]).find('.form-control').attr('data-name')].push($(inputArr[j]).val());
            }
            if (!params.times[0]) {
                params.times[0] = formatDate(new Date());
                $('#' + _this.attr('data-id') + ' input[name="start_time"]').val(params.times[0]);
            }
        }
    }
    ;
    params.page = 1;  //页码为1
    if (checkName(params.name)) {
        if (checkPhone(params.phone)) {
            if (checkTwoDate(params.times[0], params.times[1])) {
                return params;
            } else {
                alertModel('', '开始时间要早于结束时间！');
                return false;
            }
        } else {
            alertModel('', '请输入正确的手机号！');
            return false;
        }
    } else {
        alertModel('', '姓名长度不能超过16个字符！');
        return false;
    }
};
//获取查询搜索条件方法  end

